
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>21. 扩展正则表达式 · Sed-and-Awk-101-Hacks</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="devops007">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-terminal/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="substitution.html" />
    
    
    <link rel="prev" href="syntax.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://linuxer.io" target="_blank" class="custom-link">Home</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    序言
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第一章 Sed语法和基本命令</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../part1/syntax.html">
            
                <a href="../part1/syntax.html">
            
                    
                    1. Sed命令语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../part1/script.html">
            
                <a href="../part1/script.html">
            
                    
                    2. Sed脚本流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../part1/pattern.html">
            
                <a href="../part1/pattern.html">
            
                    
                    3. 打印模式空间(p 命令)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../part1/delete.html">
            
                <a href="../part1/delete.html">
            
                    
                    4. 删除行(d 命令)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../part1/write.html">
            
                <a href="../part1/write.html">
            
                    
                    5. 模式空间写入文件(w 命令)
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二章 Sed替换命令</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../part2/syntax.html">
            
                <a href="../part2/syntax.html">
            
                    
                    6. Sed替换命令语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../part2/global.html">
            
                <a href="../part2/global.html">
            
                    
                    7. 全局标志（g flag）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../part2/number.html">
            
                <a href="../part2/number.html">
            
                    
                    8. 数字标志（1,2,3...flag）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../part2/print.html">
            
                <a href="../part2/print.html">
            
                    
                    9. 打印标志（p falg）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../part2/write.html">
            
                <a href="../part2/write.html">
            
                    
                    10. 写标志（w falg）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../part2/ignore.html">
            
                <a href="../part2/ignore.html">
            
                    
                    11. 忽略大小写标志（i falg）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../part2/exec.html">
            
                <a href="../part2/exec.html">
            
                    
                    12. 执行标志（e falg）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="../part2/combine.html">
            
                <a href="../part2/combine.html">
            
                    
                    13. sed替换标志组合使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="../part2/delimiter.html">
            
                <a href="../part2/delimiter.html">
            
                    
                    14. sed替换定界符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.11" data-path="../part2/multiple.html">
            
                <a href="../part2/multiple.html">
            
                    
                    15. 多个替换命令作用到同一行上
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.12" data-path="../part2/pattern.html">
            
                <a href="../part2/pattern.html">
            
                    
                    16. &魅力 - 引用匹配模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.13" data-path="../part2/single_group.html">
            
                <a href="../part2/single_group.html">
            
                    
                    17. 分组替换（单一组）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.14" data-path="../part2/multiple_group.html">
            
                <a href="../part2/multiple_group.html">
            
                    
                    18. 分组替换（多组）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.15" data-path="../part2/string.html">
            
                <a href="../part2/string.html">
            
                    
                    19.  Gnu Sed {replacement-string}标志
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三章 Sed正则表达式</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="./">
            
                <a href="./">
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="syntax.html">
            
                <a href="syntax.html">
            
                    
                    20. 正则表达式基本原理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="4.3" data-path="additional.html">
            
                <a href="additional.html">
            
                    
                    21. 扩展正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="substitution.html">
            
                <a href="substitution.html">
            
                    
                    22. Sed基于正则表达式的替换
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四章 Sed的执行</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../part4/multiplecmd.html">
            
                <a href="../part4/multiplecmd.html">
            
                    
                    23. 命令行中的多个Sed命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../part4/script.html">
            
                <a href="../part4/script.html">
            
                    
                    24. Sed脚本
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../part4/comment.html">
            
                <a href="../part4/comment.html">
            
                    
                    25. Sed注释
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../part4/interpreter.html">
            
                <a href="../part4/interpreter.html">
            
                    
                    26. Sed解释执行
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../part4/modifying.html">
            
                <a href="../part4/modifying.html">
            
                    
                    27. 直接将修改应用到输入文件
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五章 Sed扩展命令</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../part5/append.html">
            
                <a href="../part5/append.html">
            
                    
                    28. 行后追加（a 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="../part5/insert.html">
            
                <a href="../part5/insert.html">
            
                    
                    29. 行前追加（i 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="../part5/change.html">
            
                <a href="../part5/change.html">
            
                    
                    30. 改变行（c 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="../part5/combine.html">
            
                <a href="../part5/combine.html">
            
                    
                    31. a、i、c 命令结合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="../part5/hidden.html">
            
                <a href="../part5/hidden.html">
            
                    
                    32. 打印隐藏字符（l 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="../part5/linenumber.html">
            
                <a href="../part5/linenumber.html">
            
                    
                    33. 打印行号（= 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="../part5/case.html">
            
                <a href="../part5/case.html">
            
                    
                    34. 改变大小写（y 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.9" data-path="../part5/multiple.html">
            
                <a href="../part5/multiple.html">
            
                    
                    35. 指定多个输入文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.10" data-path="../part5/quit.html">
            
                <a href="../part5/quit.html">
            
                    
                    36. 退出（q 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.11" data-path="../part5/read.html">
            
                <a href="../part5/read.html">
            
                    
                    37. 读取额外文件（r 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.12" data-path="../part5/unix.html">
            
                <a href="../part5/unix.html">
            
                    
                    38. 模拟Unix命令（cat、grep、head）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.13" data-path="../part5/option.html">
            
                <a href="../part5/option.html">
            
                    
                    39. sed命令行参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.14" data-path="../part5/patter.html">
            
                <a href="../part5/patter.html">
            
                    
                    40. 打印模式空间（n 命令）
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第六章 Sed的保持空间和模式空间命令</li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../part6/swap.html">
            
                <a href="../part6/swap.html">
            
                    
                    41. 交换模式空间与保持空间（x 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="../part6/copyp2h.html">
            
                <a href="../part6/copyp2h.html">
            
                    
                    42. 拷贝模式空间到保持空间（h 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="../part6/appendp2h.html">
            
                <a href="../part6/appendp2h.html">
            
                    
                    43. 追加模式空间到保持空间（H 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="../part6/copyh2p.html">
            
                <a href="../part6/copyh2p.html">
            
                    
                    44. 拷贝保持空间到模式空间（g 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="../part6/appendh2p.html">
            
                <a href="../part6/appendh2p.html">
            
                    
                    45. 追加保持空间到模式空间（G 命令）
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第七章 Sed多命令行和循环</li>
        
        
    
        <li class="chapter " data-level="8.1" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="../part7/append.html">
            
                <a href="../part7/append.html">
            
                    
                    46. 追加下一行到模式空间（N 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="../part7/print.html">
            
                <a href="../part7/print.html">
            
                    
                    47. 在MultiLine中打印第一行（P 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="../part7/delete.html">
            
                <a href="../part7/delete.html">
            
                    
                    48. 在MultiLine中删除第一行（D 命令）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="../part7/loop_and_branch.html">
            
                <a href="../part7/loop_and_branch.html">
            
                    
                    49. 循环和分支（b 命令和 ：label）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="../part7/loop_t.html">
            
                <a href="../part7/loop_t.html">
            
                    
                    50. 循环使用t命令
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第八章 Awk语法和基本命令</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../part8/README.md">
            
                <span>
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="../part8/syntax.md">
            
                <span>
            
                    
                    51. Awk命令语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="../part8/structure.md">
            
                <span>
            
                    
                    52. Awk程序结构（BEGIN, body, END, block）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="../part8/print.md">
            
                <span>
            
                    
                    53. 打印命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="../part8/regex.md">
            
                <span>
            
                    
                    54. 模式匹配
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第九章 Awk内置变量</li>
        
        
    
        <li class="chapter " data-level="10.1" data-path="../part9/README.md">
            
                <span>
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="../part9/input-field-sep.md">
            
                <span>
            
                    
                    55. FS - 输入字段分隔符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="../part9/output-field-sep.md">
            
                <span>
            
                    
                    56. OFS - 输出字段分隔符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="../part9/record-sep.md">
            
                <span>
            
                    
                    57. RS - 记录分隔符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="../part9/output-record-sep.md">
            
                <span>
            
                    
                    58. ORS - 输出记录分隔符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="../part9/record-num.md">
            
                <span>
            
                    
                    59. NR - 当前记录数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="../part9/filename.md">
            
                <span>
            
                    
                    60. FILENAME - 当前输入文件名
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.8" data-path="../part9/file-record-num.md">
            
                <span>
            
                    
                    61. FNR - 文件总记录数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第十章 Awk变量和运算符</li>
        
        
    
        <li class="chapter " data-level="11.1" data-path="../part10/README.md">
            
                <span>
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.2" data-path="../part10/variable.md">
            
                <span>
            
                    
                    62. 变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.3" data-path="../part10/unary-op.md">
            
                <span>
            
                    
                    63. 一元运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.4" data-path="../part10/arithmetic-op.md">
            
                <span>
            
                    
                    64. 算术运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.5" data-path="../part10/string-op.md">
            
                <span>
            
                    
                    65. 字符串运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.6" data-path="../part10/assigment-op.md">
            
                <span>
            
                    
                    66. 赋值运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.7" data-path="../part10/compare-op.md">
            
                <span>
            
                    
                    67. 比较运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.8" data-path="../part10/regex-op.md">
            
                <span>
            
                    
                    68. 正则表达式运算符
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第十一章 Awk条件和循环语句</li>
        
        
    
        <li class="chapter " data-level="12.1" data-path="../part11/README.md">
            
                <span>
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="../part11/if.md">
            
                <span>
            
                    
                    69. If 条件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.3" data-path="../part11/if-else.md">
            
                <span>
            
                    
                    70. If-Else 条件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.4" data-path="../part11/while.md">
            
                <span>
            
                    
                    71. While 循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.5" data-path="../part11/do-while.md">
            
                <span>
            
                    
                    72. Do-While 循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.6" data-path="../part11/for.md">
            
                <span>
            
                    
                    73. For 循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.7" data-path="../part11/break.md">
            
                <span>
            
                    
                    74. Break 终止循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.8" data-path="../part11/continue.md">
            
                <span>
            
                    
                    75. Continue 继续循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.9" data-path="../part11/exit.md">
            
                <span>
            
                    
                    76. Exit 退出
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第十二章 Awk内置数组</li>
        
        
    
        <li class="chapter " data-level="13.1" data-path="../part12/README.md">
            
                <span>
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="13.2" data-path="../part12/assigning-array.md">
            
                <span>
            
                    
                    77. 分配数组元素
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="13.3" data-path="../part12/refer-array.md">
            
                <span>
            
                    
                    78. 引用数组元素
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="13.4" data-path="../part12/loop-access-array.md">
            
                <span>
            
                    
                    79. 循环遍历数组元素
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="13.5" data-path="../part12/delete-array.md">
            
                <span>
            
                    
                    80. 删除数组元素
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="13.6" data-path="../part12/multi-dimensional-array.md">
            
                <span>
            
                    
                    81. 多维数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="13.7" data-path="../part12/subscript-sep.md">
            
                <span>
            
                    
                    82. SUBSEP - 下标分隔符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="13.8" data-path="../part12/sort-array-by-value.md">
            
                <span>
            
                    
                    83. asort - 数组值排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="13.9" data-path="../part12/sort-array-by-index.md">
            
                <span>
            
                    
                    84. asorti - 数组索引排序
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第十三章 Awk附加命令</li>
        
        
    
        <li class="chapter " data-level="14.1" data-path="../part13/README.md">
            
                <span>
            
                    
                    说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.2" data-path="../part13/printf.md">
            
                <span>
            
                    
                    85. printf - 格式化打印
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.3" data-path="../part13/number-function.md">
            
                <span>
            
                    
                    86. 内置数学函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.4" data-path="../part13/random-number.md">
            
                <span>
            
                    
                    87. 随机数构造
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.5" data-path="../part13/string-function.md">
            
                <span>
            
                    
                    88. 通用字符串函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.6" data-path="../part13/gawk-nawk.md">
            
                <span>
            
                    
                    89. GAWK / NAWK 字符串函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.7" data-path="../part13/gawk-string-function.md">
            
                <span>
            
                    
                    90. GAWK 字符串函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.8" data-path="../part13/argument-processing.md">
            
                <span>
            
                    
                    91. 参数处理（ARGC，ARGV，ARGIND）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.9" data-path="../part13/ofmt-variable.md">
            
                <span>
            
                    
                    92. OFMT - 内置变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.10" data-path="../part13/gawk-built-in-variable.md">
            
                <span>
            
                    
                    93. GAWK内置环境变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.11" data-path="../part13/pgawk.md">
            
                <span>
            
                    
                    94. pgawk - Awk配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.12" data-path="../part13/bit-manipulation.md">
            
                <span>
            
                    
                    95. 位操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.13" data-path="../part13/user-defined-funtion.md">
            
                <span>
            
                    
                    96. 用户自定义函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.14" data-path="../part13/internationalization.md">
            
                <span>
            
                    
                    97. 语言无关输出（国际化）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.15" data-path="../part13/communication.md">
            
                <span>
            
                    
                    98. 双向通信
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.16" data-path="../part13/system-function.md">
            
                <span>
            
                    
                    99. 系统函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.17" data-path="../part13/timestamp.md">
            
                <span>
            
                    
                    100. 时间戳函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="14.18" data-path="../part13/getline-command.md">
            
                <span>
            
                    
                    101. getline 命令
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >21. 扩展正则表达式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-anchor"></i><ul><li><span class="title-icon "></span><a href="#or-&#x64CD;&#x4F5C;&#x7B26;&#xFF08;&#xFF09;"><b>1. </b>OR &#x64CD;&#x4F5C;&#x7B26;&#xFF08;|&#xFF09;</a></li><li><span class="title-icon "></span><a href="#&#x786E;&#x5207;&#x51FA;&#x73B0;&#x7684;&#x6B21;&#x6570;m&#xFF08;m&#xFF09;"><b>2. </b>&#x786E;&#x5207;&#x51FA;&#x73B0;&#x7684;&#x6B21;&#x6570;M&#xFF08;{m}&#xFF09;</a></li><li><span class="title-icon "></span><a href="#m--n-&#x6B21;&#x51FA;&#x73B0;&#xFF08;mn&#xFF09;"><b>3. </b>M ~ N &#x6B21;&#x51FA;&#x73B0;&#xFF08;{m,n}&#xFF09;</a></li><li><span class="title-icon "></span><a href="#&#x5355;&#x8BCD;&#x8FB9;&#x754C;&#xFF08;b&#xFF09;"><b>4. </b>&#x5355;&#x8BCD;&#x8FB9;&#x754C;&#xFF08;\b&#xFF09;</a></li><li><span class="title-icon "></span><a href="#&#x53CD;&#x5411;&#x5F15;&#x7528;&#xFF08;n&#xFF09;"><b>5. </b>&#x53CD;&#x5411;&#x5F15;&#x7528;&#xFF08;\n&#xFF09;</a></li></ul></div><a href="#or-&#x64CD;&#x4F5C;&#x7B26;&#xFF08;&#xFF09;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="or-&#x64CD;&#x4F5C;&#x7B26;&#xFF08;&#xFF09;"><a name="or-&#x64CD;&#x4F5C;&#x7B26;&#xFF08;&#xFF09;" class="anchor-navigation-ex-anchor" href="#or-&#x64CD;&#x4F5C;&#x7B26;&#xFF08;&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>1. OR &#x64CD;&#x4F5C;&#x7B26;&#xFF08;|&#xFF09;</h1>
<p>&#x7BA1;&#x9053;&#x7B26;&#xFF08;|&#xFF09;&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x5728;&#x4F4D;&#x7F6E;&#x4E0A;&#x7684;&#x4E24;&#x4E2A;&#x6574;&#x4F53;&#x4E2D;&#x7684;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;&#xFF0C;&#x201C;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;1|&#x5B50;&#x8868;&#x8FBE;&#x5F0F;2&#x201D;&#x5339;&#x914D;&#x5176;&#x4E2D;&#x7684;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;1&#x6216;&#x8005;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;2</p>
<p><strong>&#x6253;&#x5370;&#x5305;&#x542B;101&#x6216;&#x8005;102&#x7684;&#x884C;</strong></p>
<pre><code>$ sed -n &apos;/101\|102/ p&apos; employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
</code></pre><p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x201C;|&#x201D;&#x7B26;&#x53F7;&#x7528;&#x201C;\&#x201D;&#x6765;&#x8F6C;&#x8BD1;&#x3002;</p>
<p><strong>&#x6253;&#x5370;&#x5305;&#x542B;&#x5B57;&#x7B26;2-3&#x4E2D;&#x7684;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;&#x6216;&#x8005;&#x5305;&#x542B;&#x5B57;&#x7B26;&#x4E32;105&#x7684;&#x884C;</strong></p>
<pre><code>$ sed -n &apos;/[2-3]\|105/ p&apos; employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
105,Jane Miller,Sales Manager
</code></pre><h1 id="&#x786E;&#x5207;&#x51FA;&#x73B0;&#x7684;&#x6B21;&#x6570;m&#xFF08;m&#xFF09;"><a name="&#x786E;&#x5207;&#x51FA;&#x73B0;&#x7684;&#x6B21;&#x6570;m&#xFF08;m&#xFF09;" class="anchor-navigation-ex-anchor" href="#&#x786E;&#x5207;&#x51FA;&#x73B0;&#x7684;&#x6B21;&#x6570;m&#xFF08;m&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>2. &#x786E;&#x5207;&#x51FA;&#x73B0;&#x7684;&#x6B21;&#x6570;M&#xFF08;{m}&#xFF09;</h1>
<p>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x540E;&#x8DDF;{m}&#xFF0C;&#x5339;&#x914D;&#x524D;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;m&#x6B21;&#x3002;</p>
<p>&#x8BF7;&#x521B;&#x5EFA;&#x5982;&#x4E0B;numbers.txt&#x6587;&#x4EF6;&#x7528;&#x4E8E;&#x4F8B;&#x5B50;&#x6F14;&#x793A;&#x3002;</p>
<pre><code>$ vi numbers.txt
1
12
123
1234
12345
123456
</code></pre><p><strong>&#x6253;&#x5370;&#x5305;&#x542B;&#x4EFB;&#x610F;&#x4E2A;&#x8FDE;&#x7EED;&#x6570;&#x5B57;&#x5B57;&#x7B26;&#x7684;&#x884C;&#xFF08;&#x5C06;&#x4F1A;&#x6253;&#x5370;&#x6240;&#x6709;&#x884C;&#xFF09;</strong></p>
<pre><code>$ sed -n &apos;/[0-9]/ p&apos; numbers.txt
1
12
123
1234
12345
123456
</code></pre><p><strong>&#x6253;&#x5370;&#x53EA;&#x7531;&#x8FDE;&#x7EED;5&#x4F4D;&#x6570;&#x5B57;&#x5B57;&#x7B26;&#x7EC4;&#x6210;&#x7684;&#x884C;</strong></p>
<pre><code>$ sed -n &apos;/^[0-9]\{5\}$/ p&apos; numbers.txt
12345
</code></pre><h1 id="m--n-&#x6B21;&#x51FA;&#x73B0;&#xFF08;mn&#xFF09;"><a name="m--n-&#x6B21;&#x51FA;&#x73B0;&#xFF08;mn&#xFF09;" class="anchor-navigation-ex-anchor" href="#m--n-&#x6B21;&#x51FA;&#x73B0;&#xFF08;mn&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>3. M ~ N &#x6B21;&#x51FA;&#x73B0;&#xFF08;{m,n}&#xFF09;</h1>
<p>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x540E;&#x8DDF;{m,n}&#x8868;&#x793A;&#x5339;&#x914D;&#x524D;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x81F3;&#x5C11;m&#x6B21;&#xFF0C;&#x6700;&#x591A;&#x4E0D;&#x8D85;&#x8FC7;n&#x6B21;&#xFF08;&#x5305;&#x542B;n&#xFF09;&#xFF0C;&#x8BE5;m&#x548C;n&#x7684;&#x503C;&#x5FC5;&#x987B;&#x662F;&#x975E;&#x8D1F;&#x503C;&#xFF0C;&#x5E76;&#x4E14;&#x8981;&#x5C0F;&#x4E8E;255&#x3002;</p>
<p><strong>&#x6253;&#x5370;&#x5305;&#x542B;&#x8FDE;&#x7EED;&#x6570;&#x5B57;&#x5B57;&#x7B26;&#x81F3;&#x5C11;3&#x4E2A;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x8D85;&#x8FC7;5&#x4E2A;&#x7684;&#x884C;</strong></p>
<pre><code>$ sed -n &apos;/^[0-9]\{3,5\}$/ p&apos; numbers.txt
123
1234
12345
</code></pre><p>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x540E;&#x9762;&#x8DDF;{m,}&#x662F;&#x4E00;&#x79CD;&#x7279;&#x6B8A;&#x60C5;&#x51B5;&#xFF0C;&#x8868;&#x793A;&#x5339;&#x914D;&#x524D;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x81F3;&#x5C11;&#x8FDE;&#x7EED;&#x51FA;&#x73B0;m&#x6B21;&#xFF0C;&#x6CA1;&#x6709;&#x6700;&#x9AD8;&#x9650;&#x5236;&#xFF0C;&#x540C;&#x7406;{,n}&#x8868;&#x793A;&#x5339;&#x914D;&#x524D;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x8FDE;&#x7EED;&#x51FA;&#x73B0;&#x6B21;&#x6570;&#x4E0D;&#x8D85;&#x8FC7;n&#x6B21;&#xFF08;&#x5305;&#x542B;n&#x6B21;&#xFF09;</p>
<h1 id="&#x5355;&#x8BCD;&#x8FB9;&#x754C;&#xFF08;b&#xFF09;"><a name="&#x5355;&#x8BCD;&#x8FB9;&#x754C;&#xFF08;b&#xFF09;" class="anchor-navigation-ex-anchor" href="#&#x5355;&#x8BCD;&#x8FB9;&#x754C;&#xFF08;b&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>4. &#x5355;&#x8BCD;&#x8FB9;&#x754C;&#xFF08;\b&#xFF09;</h1>
<p>\b&#x88AB;&#x7528;&#x6765;&#x5339;&#x914D;&#x5355;&#x8BCD;&#x8FB9;&#x754C;&#x3002;\b&#x5339;&#x914D;&#x5355;&#x8BCD;&#x4EE5;&#xFF08;\bxx&#xFF09;&#x5F00;&#x5934;&#x6216;&#x4EE5;&#xFF08;xx\b&#xFF09;&#x7ED3;&#x5C3E;&#x7684;&#x4EFB;&#x4F55;&#x5B57;&#x7B26;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x201C;\bthe\b&#x201D;&#x5C06;&#x53EA;&#x4F1A;&#x5339;&#x914D;&#x5355;&#x8BCD;the&#x800C;&#x4E0D;&#x662F;&#x5176;&#x4ED6;&#xFF0C;&quot;\bthe&quot;&#x5C06;&#x4F1A;&#x5339;&#x914D;the&#x6216;&#x8005;the&#x5F00;&#x5934;&#x7684;&#x5355;&#x8BCD;&#x3002;</p>
<p>&#x521B;&#x5EFA;&#x5982;&#x4E0B;&#x6D4B;&#x8BD5;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>$ cat words.txt
word matching using: the
word matching using: thethe
word matching using: they
</code></pre><p><strong>&#x5339;&#x914D;&#x5305;&#x542B;&#x5355;&#x8BCD;&#x201C;the&#x201D;&#x7684;&#x884C;</strong></p>
<pre><code>$ sed -n &apos;/\bthe\b/ p&apos; words.txt
word matching using: the
</code></pre><p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5728;&#x672B;&#x5C3E;&#x6307;&#x5B9A;\b&#xFF0C;&#x5219;&#x4F1A;&#x5339;&#x914D;&#x6240;&#x6709;&#x884C;&#x3002;</p>
<p><strong>&#x5339;&#x914D;&#x5305;&#x542B;&#x201C;the&#x201D;&#x5F00;&#x5934;&#x5355;&#x8BCD;&#x7684;&#x884C;</strong></p>
<pre><code>$ sed -n &apos;/\bthe/ p&apos; words.txt
word matching using: the
word matching using: thethe
word matching using: they
</code></pre><h1 id="&#x53CD;&#x5411;&#x5F15;&#x7528;&#xFF08;n&#xFF09;"><a name="&#x53CD;&#x5411;&#x5F15;&#x7528;&#xFF08;n&#xFF09;" class="anchor-navigation-ex-anchor" href="#&#x53CD;&#x5411;&#x5F15;&#x7528;&#xFF08;n&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>5. &#x53CD;&#x5411;&#x5F15;&#x7528;&#xFF08;\n&#xFF09;</h1>
<p>&#x53CD;&#x5411;&#x5F15;&#x7528;&#x53EF;&#x4EE5;&#x5C06;&#x8868;&#x8FBE;&#x5F0F;&#x5206;&#x7EC4;&#x4EE5;&#x4F9B;&#x8FDB;&#x4E00;&#x6B65;&#x4F7F;&#x7528;</p>
<p><strong>&#x5339;&#x914D;&#x5305;&#x542B;&#x5355;&#x8BCD;&#x201C;the&#x201D;&#x4E14;&#x91CD;&#x590D;&#x51FA;&#x73B0;2&#x6B21;&#x7684;&#x884C;</strong></p>
<pre><code>sed -n &apos;/\(the\)\1/ p&apos; words.txt
word matching using: thethe
</code></pre><p>&#x4F7F;&#x7528;&#x76F8;&#x540C;&#x7684;&#x903B;&#x8F91;&#xFF0C;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x201C;([0-9])\1&#x201D;&#x5339;&#x914D;&#x8FDE;&#x7EED;&#x4E24;&#x4F4D;&#x6570;&#x5B57;&#x5B57;&#x7B26;&#x90FD;&#x662F;&#x76F8;&#x540C;&#x6570;&#x5B57;&#x7684;&#x884C;&#xFF0C;&#x4F8B;&#x5982;:11, 22, 33</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="syntax.html" class="navigation navigation-prev " aria-label="Previous page: 20. 正则表达式基本原理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="substitution.html" class="navigation navigation-next " aria-label="Next page: 22. Sed基于正则表达式的替换">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"21. 扩展正则表达式","level":"4.3","depth":1,"next":{"title":"22. Sed基于正则表达式的替换","level":"4.4","depth":1,"path":"part3/substitution.md","ref":"part3/substitution.md","articles":[]},"previous":{"title":"20. 正则表达式基本原理","level":"4.2","depth":1,"path":"part3/syntax.md","ref":"part3/syntax.md","articles":[]},"dir":"ltr"},"config":{"plugins":["emphasize","theme-comscore","-prism","-prism-themes","highlight","simple-page-toc","anchors","anchor-navigation-ex","terminal","-splitter","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"prism":{"css":["prism-themes/themes/prism-duotone-sea.css"]},"emphasize":{},"livereload":{},"simple-page-toc":{"maxDepth":3,"skipFirstH1":true},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"mode":"float","pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"isRewritePageTitle":true,"showLevel":true,"isShowTocTitleIcon":true,"printLog":false,"multipleH1":true,"associatedWithSummary":true,"float":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":true,"tocLevel1Icon":"fa fa-hand-o-right","tocLevel2Icon":"fa fa-hand-o-right","tocLevel3Icon":"fa fa-hand-o-right"}},"theme-comscore":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"terminal":{"copyButtons":true,"fade":false,"style":"classic"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{}},"theme":"default","author":"devops007","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Sed-and-Awk-101-Hacks","links":{"sidebar":{"Home":"https://linuxer.io"}},"gitbook":"*"},"file":{"path":"part3/additional.md","mtime":"2018-05-29T01:56:34.767Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-12T09:00:04.642Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-terminal/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

